package com.example.demovote.service.impl;

import com.example.demovote.entity.Items;
import com.example.demovote.entity.Vote;
import com.example.demovote.mapper.VoteMapper;
import com.example.demovote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demovote.service.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 投票系统-题目表-2 服务实现类
 * </p>
 *
 * @author ly
 * @since 2024-03-18
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {

    @Resource
    ItemsServiceImpl itemsService;

    @Override
    @Transactional
    public void create(Vote vote, String[] inames) throws ServiceException {
        // 判断
        if(inames==null || inames.length==0){
            throw new ServiceException(-1, "请输入选项");
        }

        save(vote);
        vote.getId();
        Items items = new Items();
        items.setVid(vote.getId());
        for (String iname : inames) {
            if(iname==null || iname.isEmpty()){
                throw new ServiceException(-2, "选项不能为空");
            }
            items.setId(null);
            items.setIname(iname);
            itemsService.save(items);
        }

    }
}
